<?php

namespace app\common\model;

use think\Collection;
use PDOStatement;
use think\Db;
use think\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\db\exception\DataNotFoundException;
use think\exception\HttpResponseException;
use think\Model;

class QuestionGroup extends Model
{
    // 表名
    protected $name = 'question_group';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;


    /**
     * 刷题列表
     * @param mixed $type  1=全部,2=传二级分类id
     * @param mixed $cate_id 
     */
    public function getList($where, $type = 0, $user_id = 0)
    {

        $list = $this->where(function ($query) use ($where, $type) {
            # 查询二级分类刷题
            if ($type == 2) {
                $query->where('question_cate_id', $where['cate_id']);
            }
            # 查询全部 根据一级分类ID
            if ($type == 1) {
                $query->where('question_cate_pid', $where['cate_id']);
            }
            # 首页查询
            if (isset($where['question_group_ids']) && !empty($where['question_group_ids'])) {
                $query->whereIn('id', $where['question_group_ids']);
            }
            # 首页模块查询
            if (isset($where['question_cate_id2']) && !empty($where['question_cate_id2'])) {
                $query->where('id', $where['question_cate_id2']);
            }
            $query->where('status', 'normal');
        })
            ->field("id as question_group_id,name,image,num")
            ->order('id asc')
            ->select();
        if (empty($list)) fail('暂无数据', [], 1);

        return self::dataArray($list, $user_id);
    }


    /**
     * 刷题详情
     * @param mixed $cate_id 
     * @param int $user_id 
     */
    public function getDetails($cate_id, $user_id = 0)
    {
        $cateInfo = Db::name('question_cate')->field('id,name,image')->where('id', $cate_id)->find();
        if (empty($cateInfo)) fail("分类不存在");
        $question_cate_id3 = $cateInfo['id'];
        $list = $this->where(function ($query) use ($question_cate_id3) {
            $query->where('question_cate_id3', $question_cate_id3);
            $query->where('status', 'normal');
        })
            ->field("id as question_group_id,name,image,num")
            ->order('id asc')
            ->select();
        return [
            'cate_info' => $cateInfo,
            'list' => empty($list) ? [] : self::dataArray($list, $user_id),
        ];
    }


    /**
     * 数据整理
     * @param mixed $list 
     * @param mixed $user_id 
     * @return mixed 
     */
    public function dataArray($list, $user_id)
    {
        # 查询用户 那些刷题已添加
        $user_question = [];
        if ($user_id > 0) {
            $user_question = (new UserQuestion())->getUserQusetionAddIs($user_id);
        }
        if (!empty($list)) {
            foreach ($list as $key => $value) {
                # 判定是否添加首页
                if (in_array($value['question_group_id'], $user_question)) {
                    $list[$key]['is_add'] = 1;  # 已添加首页
                } else {
                    $list[$key]['is_add'] = 0;  # 未添加首页
                }
                # 查询组卷刷题 数量
                $questionCount = Db::name('question_paper')->where('question_group_id', $value['question_group_id'])->count();
                $list[$key]['question_count'] = $questionCount;
                # 查询用户答题数数量
                $userAnswersCount = Db::name('user_answers')->where('user_id', $user_id)->where(['question_group_id' => $value['question_group_id'], 'type' => '0'])->whereIn('is_right', ['0', '1', '3'])->count();
                $list[$key]['user_answers_count'] = $userAnswersCount;
            }
        }
        return $list;
    }
}
